#include "./../includes/logger.h"

fila<std::string>
logger::allEntries;

void
logger::newEntry( const std::string _entry )
{
	std::string
	tmpTime( this->getLocalTime() );
	
	std::clog << "[" << tmpTime << "] " << _entry << std::endl ;
	
	logger::allEntries.push( "[" + tmpTime + "] " + _entry );
	
}

void
logger::saveTo( const char* _filename )
{
	
	tm*
	tmpTm;
	
	std::string
	endMsg;

	std::ofstream
	logfile( _filename , std::ios::out | std::ios::trunc );

	while( ! logger::allEntries.isEmpty() )
	{
		logfile << logger::allEntries.getInfo() << std::endl;

		logger::allEntries.pop();
	}

	time( &this->rawtime );

	tmpTm = localtime( &this->rawtime );

	endMsg = "log ended at ";

	endMsg.append( asctime( tmpTm ) );

	logfile << std::endl << endMsg;

	logfile.close();

}


/*************
 * 			 *
 *  Private  *
 * 			 *
 *************/

std::string
logger::getLocalTime()
{
	tm*
	tmpTm;
	
	time( &this->rawtime );
	
	tmpTm = localtime( &this->rawtime );
	
	this->localTime = asctime( tmpTm );
	
	return localTime.substr( 11 , 8 ); 
}

logger::logger()
{
	tm*
	tmpTm;
	
	std::string
	initMsg;
	
	time( &this->rawtime );
	
	tmpTm = localtime( &this->rawtime );
	
	initMsg = "log started at ";
	
	initMsg.append( asctime( tmpTm ) );
	
	logger::allEntries.push( initMsg );
}

logger::~logger()
{
}
